﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LabLibary.ExtractData
{
    public static class CobasE411
    {
        public static List<ObjResults> ExtactData(string fileName)
        {

            DateTime _Ngay = DateTime.Now;
            string _May = "16";
            string _SID = string.Empty;
            string _LabMaChineId = string.Empty;
            string _Value = string.Empty;

            List<ObjResults> _lstResults = new List<ObjResults>();
            ObjResults _objResults = new ObjResults();

            string result = string.Empty;
            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        try
                        {
                            line = line.Remove(0, 2);

                            if (line[0] == 'O')
                            {
                                string[] _tmpSplit = line.Split('|');
                                result += "ID: " + _tmpSplit[2].Trim() + "\n";

                                _SID = _tmpSplit[2].Trim();

                                string _date = _tmpSplit[6].Trim();
                                string _nam = _date.Remove(4, 10);
                                string _thang = _date.Replace(_nam, string.Empty).Remove(2, 8);
                                string _ngay = _date.Replace(_nam, string.Empty).Replace(_thang, string.Empty).Remove(2,6);

                                _Ngay = new DateTime(int.Parse(_nam), int.Parse(_thang), int.Parse(_ngay), 0, 0, 0);
                            }

                            if (line[0] == 'R')
                            {
                                string[] _tmpSplit = line.Split('|');
                                //result += "LabID: " + _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("^0", string.Empty);
                                //result += " - Value: " + _tmpSplit[3].Trim() + "\n";


                                _LabMaChineId = _tmpSplit[2].Trim().Replace("^0", string.Empty).Replace("^", string.Empty);
                                _Value = _tmpSplit[3].Trim().Replace("-1^", string.Empty);

                                //_lstResults.Add(new ObjResults(_SID, _LabMaChineId, _Value));
                                _lstResults.Add(new ObjResults(_Ngay, _SID, _May, _LabMaChineId, _Value, DateTime.Now));
                            }
                        }
                        catch { }
                    }

                    try
                    {
                        sr.Close();
                        //File.Delete(fileName);
                    }
                    catch { }

                    return _lstResults;
                }
            }
            catch
            {
                result = null;
            }
            return _lstResults;
        }
    }
}
